package com.guodi.model.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guodi.common.utils.Asserts;
import com.guodi.common.utils.UuidUtil;
import com.guodi.core.boot.ctrl.BaseController;
import com.guodi.core.tool.api.R;
import com.guodi.model.entity.*;
import com.guodi.model.service.*;
import com.guodi.model.vo.ApiIdtProblemVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @描述：更新评估-项目管理表控制类
 * @作者: 徐金洲
 * @日期: 2023-05-05
 */
@RestController
@RequestMapping("/UeProjectManger")
@Api(value = "API - UeProjectMangerController", tags = "项目管理表控制类")
public class UeProjectMangerController extends BaseController {


    @Resource
    public IUeProjectMangerService ueProjectMangerService;

    @Resource
    public IUeUpdateRecordsService ueUpdateRecordsService;

    @Resource
    public IUeProjectAttachmentService ueProjectAttachmentService;

    @Resource
    public ICmeDiagnosisService cmeDiagnosisService;
    @Resource
    public IIdtFillProblemService iIdtFillProblemService;
    @Resource
    public IUeProblemRecordsService ueProblemRecordsService;


    /**
     * @描述: 查询应对问题下拉列表
     * @入参: year, divisionCode
     * @出参: list
     * @作者: 徐金洲
     * @日期: 2023-05-08
     **/
    @PostMapping("/getProblemList")
    @ApiOperation(value = "查询应对问题下拉列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "divisionCode", value = "行政区划编码", required = true, dataType = "String", paramType = "query")
    })
    public R getProblemList(String year, String divisionCode) {
        Asserts.isEmpty(year, "年份必传");
        Asserts.isEmpty(divisionCode, "行政区划编码必传");
        //定义list接收返回值
//        List<CmeDiagnosis> list1 = null;
        List<ApiIdtProblemVo> list = null;
        if(org.apache.commons.lang.StringUtils.isNotBlank(year) && org.apache.commons.lang.StringUtils.isNotBlank(divisionCode)){
//            list1 = cmeDiagnosisService.getProblemList(year,divisionCode);
            list = iIdtFillProblemService.getProblemList(year,divisionCode);
        }
        return R.data(list);
    }


    /**
     * @描述: 分页查询列表
     * @入参: UeProjectManger
     * @出参: page
     * @作者: 徐金洲
     * @日期: 2023-05-05
     **/
    @PostMapping("/list")
    @ApiOperation(value = "分页查询列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "页数", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "查询数量", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "projectName", value = "项目名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "divisionCode", value = "行政区划", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "improveIdt", value = "改善指标", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "projectSchedule", value = "项目进度", required = false, dataType = "String", paramType = "query")
    })
    public R list(Page page, UeProjectManger ueProjectManger) {
        page = ueProjectMangerService.listByEntity(page, ueProjectManger);
        return R.data(page);
    }


    /**
     * @描述: 新增更新评估-项目管理信息
     * @入参: UeProjectManger
     * @出参: boolean
     * @作者: 徐金洲
     * @日期: 2023-05-05
     **/
    @PostMapping("/save")
    @ApiOperation(value = "新增")
    public R save(@Valid UeProjectManger ueProjectManger) {
        prepareData(ueProjectManger,true);
        ueProjectManger.setId(UuidUtil.get32UUID());
        boolean temp = ueProjectMangerService.save(ueProjectManger);
        if (temp) {
            //保存更新记录数据
            UeUpdateRecords ueUpdateRecords = new UeUpdateRecords();
            ueUpdateRecords.setId(UuidUtil.get32UUID());
            ueUpdateRecords.setProjectId(ueProjectManger.getId());
            ueUpdateRecords.setProcessedBy(this.getUser().getUserId().toString());
            ueUpdateRecords.setUpdateContent("创建项目");
            ueUpdateRecords.setUpdateTime(new Date());
            ueUpdateRecordsService.save(ueUpdateRecords);
            //记录问题
            ueProblemRecordsService.calcProblemRecords(ueProjectManger);
            return R.success("新增成功");
        }
        return R.fail("新增失败");
    }


    /**
     * @描述: 修改更新评估-项目管理信息
     * @入参: UeProjectManger
     * @出参: boolean
     * @作者: 徐金洲
     * @日期: 2023-05-05
     **/
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update(@Valid UeProjectManger ueProjectManger) throws IOException {
        prepareData(ueProjectManger, false);
        //获取处理人,用于保存更新记录
        String processedBy = this.getUser().getUserId().toString();
        boolean temp = ueProjectMangerService.update(ueProjectManger,processedBy);
        //记录问题
//        ueProblemRecordsService.calcProblemRecords(ueProjectManger);
        if (temp) {
            return R.success("修改成功");
        }
        return R.fail("修改失败");
    }


    /**
     * @描述: 删除更新评估-项目管理信息
     * @入参: UeProjectManger
     * @出参: boolean
     * @作者: 徐金洲
     * @日期: 2023-05-05
     **/
    @PostMapping("/delete")
    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "ids", value = "主键id，如果多个，英文逗号隔开", required = true, dataType = "string", paramType = "query")
    public R delete(String ids) {
        if (StringUtils.isBlank(ids)) {
            return R.fail("id不能为空!");
        }
        //将英文逗号分隔的字符串 id 转成 list
        List<String> idList = Arrays.asList(ids.trim().split(","));
        if (idList.size() > 0) {
            //删除项目管理信息
            boolean temp0 = ueProjectMangerService.removeByIds(idList);
            //删除更新记录信息(根据项目id,删除更新记录信息)
            boolean temp1 = false;
            for(String projectId : idList){
                temp1 = ueUpdateRecordsService.deleteByProjectId(projectId);
            }
            //删除附件材料信息(根据项目id,删除附件材料信息)
            boolean temp2 = false;
            for(String projectId : idList){
                LambdaQueryWrapper<UeProjectAttachment> queryWrapper = (new QueryWrapper()).lambda();
                queryWrapper.eq(UeProjectAttachment::getProjectId, projectId);
                List<UeProjectAttachment> filelist = ueProjectAttachmentService.list(queryWrapper);
                for(UeProjectAttachment ueProjectAttachment : filelist){
                    String id = ueProjectAttachment.getId();
                    //根据id 删除 minio 上的附件再删除附件
                    ueProjectAttachmentService.deleteById(id);
                }
                //根据项目 id 删除附件材料信息
                temp2 = ueProjectAttachmentService.deleteByProjectId(projectId);
            }
            if (temp0) {
                return R.success("删除成功!");
            }
        }
        return R.fail("删除失败!");
    }



    private void prepareData(UeProjectManger ueProjectManger, boolean isSave) {
        if(isSave){
            ueProjectManger.setCreator(this.getUser().getUserId().toString());
            ueProjectManger.setCreateTime(new Date());
            //防止误修改
            ueProjectManger.setModifier(null);
            ueProjectManger.setModifyTime(null);
        }else{
            ueProjectManger.setModifier(this.getUser().getUserId().toString());
            ueProjectManger.setModifyTime(new Date());
            //防止误修改
            ueProjectManger.setCreator(null);
            ueProjectManger.setCreateTime(null);
        }
    }
}
